/*
 * Copyright © 2012 Nokia Corporation. All rights reserved.
 * Nokia and Nokia Connecting People are registered trademarks of Nokia Corporation.
 * Oracle and Java are trademarks or registered trademarks of Oracle and/or its
 * affiliates. Other product and company names mentioned herein may be trademarks
 * or trade names of their respective owners.
 * See LICENSE.TXT for license information.
 */

package com.nokia.example.explonoid.menu;

import com.nokia.example.explonoid.effects.Slideable;
import com.nokia.example.explonoid.sensors.AccelerationProvider;
import javax.microedition.lcdui.Graphics;

public class MainMenu
        extends Menu
        implements Slideable {

    public static final int ITEM_COUNT = 6;
    public static final int RESUME = 0;
    public static final int NEWGAME = 1;
    public static final int SENSORS = 2;
    public static final int SOUNDS = 3;
    public static final int EXIT = 4;
    public static final int INFO = 5;
    public final int OUT_CX;
    public final int IN_CX;
    private final ToggleMenuItem sensors;
    private final ToggleMenuItem sounds;
    private int x;
    private int y;
    private int width;
    private int cornerY;

    public MainMenu(int cornerX, int cornerY, int width, int height, Listener l, double scaling) {
        super(ITEM_COUNT, l);
        this.width = width;       
        this.cornerY = cornerY;
        setItem(RESUME, new MenuItem(loadSprite("/resume.png", 2, scaling)));
        setItem(NEWGAME, new MenuItem(loadSprite("/new_game.png", 2, scaling)));
        sensors = new ToggleMenuItem(loadSprite("/sensors.png", 4, scaling));
        if(AccelerationProvider.sensorsSupported()) {
            sensors.set(true);
        } else {
            sensors.set(false);
        }
        setItem(SENSORS, sensors);
        sounds = new ToggleMenuItem(loadSprite("/sounds.png", 4, scaling));
        setItem(SOUNDS, sounds);
        sounds.set(true);
        setItem(EXIT, new MenuItem(loadSprite("/exit.png", 2, scaling)));
        setItem(INFO, new MenuItem(loadSprite("/info.png", 2, scaling)));
        IN_CX = cornerX + width / 2;
        OUT_CX = IN_CX - width;

        x = OUT_CX;
        y = cornerY + height / 31 * 15;
        positionItemsHorizontally();
        positionItemsVertically();
    }

    /**
     * Render the menu
     */
    public void paint(Graphics g) {
        super.paint(g);
    }

    /**
     * Toggle sounds on and off
     * @return Current state. True, if on and false, if off.
     */
    public boolean toggleSounds() {
        return sounds.toggle();
    }

    /**
     * Toggle sensors on and off
     * @return Current state. True, if on and false, if off.
     */
    public boolean toggleSensors() {
        if(AccelerationProvider.sensorsSupported()) {
            sensors.toggle();
        }
        return sensors.isOn();
    }

    /**
     * Move view inwards
     */
    public boolean slideIn() {
        int distance = x - IN_CX;
        distance *= 0.8;
        x = IN_CX + distance;
        positionItemsHorizontally();
        return distance != 0;
    }

    /**
     * Move view outwards
     */
    public boolean slideOut() {
        int distance = x - OUT_CX;
        distance *= 0.8;
        x = OUT_CX + distance;
        positionItemsHorizontally();
        return distance != 0;
    }

    /**
     * Lay out menu items horizontally
     */
    public final void positionItemsHorizontally() {
        MenuItem item;
        for (int i = 0; i < ITEM_COUNT - 1; i++) {
            item = getItem(i);
            item.setHorizontalCenter(x);
        }
        item = getItem(ITEM_COUNT - 1);
        item.setPosition(x - width / 2, cornerY);
    }

    /**
     * Lay out menu items vertically
     */
    public final void positionItemsVertically() {
        int newY = y;
        MenuItem resume = getItem(RESUME);
        if(!resume.isVisible()) {
            newY -= resume.getHeight() / 2;
        }
        for (int i = 0; i < ITEM_COUNT - 1; i++) {
            MenuItem item = getItem(i);
            item.setCenter(item.getX(), newY);
            newY += item.getHeight();
        }
    }

    /**
     * Hide resume option
     */
    public void hideResume() {
        getItem(RESUME).setVisibile(false);
        positionItemsVertically();
    }

    /**
     * Show resume option
     */
    public void showResume() {
        getItem(RESUME).setVisibile(true);
        selectItem(RESUME);
        positionItemsVertically();
    }
}
